package com.beehoo.dcportal.action;

import java.util.List;

import com.beehoo.dcportal.common.Constant;
import com.beehoo.dcportal.common.base.BaseAction;
import com.beehoo.dcportal.common.base.BaseException;
import com.beehoo.dcportal.model.ComplaintBean;
import com.beehoo.dcportal.service.IComplaintServ;
import com.beehoo.dcportal.utils.CommonUtils;

public class ComplaintAction extends BaseAction {

	private static final long serialVersionUID = -1571458517189218968L;
	private IComplaintServ complaintServ;
	private ComplaintBean complaintBean;
	private List<ComplaintBean> queTypeLs;
	private List<ComplaintBean> servLineLs;
	private List<ComplaintBean> subServLineLs;
	private String servLineId;
	private String vCode;

	public String getVCode() {
		return vCode;
	}

	public void setVCode(String code) {
		vCode = code;
	}

	public String getServLineId() {
		return servLineId;
	}

	public void setServLineId(String servLineId) {
		this.servLineId = servLineId;
	}

	public List<ComplaintBean> getQueTypeLs() {
		return queTypeLs;
	}

	public void setQueTypeLs(List<ComplaintBean> queTypeLs) {
		this.queTypeLs = queTypeLs;
	}

	public List<ComplaintBean> getServLineLs() {
		return servLineLs;
	}

	public void setServLineLs(List<ComplaintBean> servLineLs) {
		this.servLineLs = servLineLs;
	}

	public List<ComplaintBean> getSubServLineLs() {
		return subServLineLs;
	}

	public void setSubServLineLs(List<ComplaintBean> subServLineLs) {
		this.subServLineLs = subServLineLs;
	}

	public ComplaintBean getComplaintBean() {
		return complaintBean;
	}

	public void setComplaintBean(ComplaintBean complaintBean) {
		this.complaintBean = complaintBean;
	}

	public void setComplaintServ(IComplaintServ complaintServ) {
		this.complaintServ = complaintServ;
	}

	

	public String execute() {
		String complaintSubmitFlag = CommonUtils.nvlToString((String) getSession().get("complaint_submit_ok"));
		getSession().remove("complaint_submit_ok");
		if(complaintSubmitFlag.equals("success")){
			setMessage(super.getText("custom.messages.success.complaint.add.success"));
		}else if(complaintSubmitFlag.equals("idCardError")){
			setMessage(super.getText("custom.messages.error.idcard.num.none"));
		}
		try {
			queTypeLs = complaintServ.queryQueType();
			servLineLs = complaintServ.queryServLine();
			if (servLineLs != null && servLineLs.size() > 0) {
				ComplaintBean bean = servLineLs.get(0);
				String servLineId = bean.getServLine();
				if(complaintBean != null && 
						!CommonUtils.nvlToString(complaintBean.getServLine()).equals("")){
					servLineId = complaintBean.getServLine();
				}
				subServLineLs = complaintServ.querySubServLine(servLineId);
			}
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return INPUT;
	}
	
	public String changeServline(){
		try {
			subServLineLs = complaintServ.querySubServLine(servLineId);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
		}
		return NONE;
	}
	
	public String submit(){
		String idCardNum = null;
		if(complaintBean != null){
			complaintBean.setStreetId(paramMap.get("streetId"));
			idCardNum = complaintBean.getAuthorIdcard();
		}
		
		//对验证码进行校验
		try {
			String rand = CommonUtils.nvlToString((String) getSession().get(Constant.SESSION_VALIDATECODE));
			getSession().remove(Constant.SESSION_VALIDATECODE);
			if(CommonUtils.nvlToString(vCode).equals("")){
				throw new BaseException("custom.messages.error.validatecode.empty");
			}
			if(!CommonUtils.nvlToString(vCode).equalsIgnoreCase(rand)){
				throw new BaseException("custom.messages.error.validatecode.error");
			}
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			return execute();
		}
		
		//对身份证号码验证
		try {
			getCommServ().validateIDCardNum(idCardNum);
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			getSession().put("complaint_submit_ok", "idCardError");
			return SUCCESS;
		}
		
		//保存
		try {
			complaintServ.addComplaint(complaintBean);
			getSession().put("complaint_submit_ok", "success");
		} catch (BaseException e) {
			setMessage(super.getText(e.getMessage()));
			return execute();
		}
		
		return SUCCESS;
	}
}
